<script>
import { addMonthCardApi, getEditMonthCardInfoApi, putEditMonthCardInfoApi } from '@/api/carCard'
export default {
  name: 'AddMonthCard',
  data() {
    return {
      carInfoFormData: {
        personName: '', // 车主姓名
        phoneNumber: '', // 联系方式
        carNumber: '', // 车牌号码
        carBrand: '' // 车辆品牌
      },
      recentCardPriceInfoUpload: {
        cardStartDate: '', // 有效时间开始
        cardEndDate: '', // 有效时间结束
        paymentAmount: '', // 支付金额
        paymentMethod: '' // 支付方式,支付宝:Alipay,微信:WeChat,线下:Cash
      },
      recentCardPriceInfo: {
        timePickerData: [], // 日期选择器的数据
        paymentAmount: '', // 支付金额
        paymentMethod: '' // 支付方式,支付宝:Alipay,微信:WeChat,线下:Cash
      },
      PAY_METHOD: [
        { name: '支付宝', value: 'Alipay' },
        { name: '微信', value: 'Wechat' },
        { name: '线下', value: 'Cash' }
      ],
      carInfoId: null,
      rechargeId: null,
      // 校验规则
      carInfoRule: {
        personName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        carNumber: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: this.validatorCarNumber, trigger: 'blur' }
        ],
        carBrand: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      recentPriceRule: {
        timePickerData: [
          { type: 'array', required: true, message: '不能为空', trigger: 'change' }
        ],
        paymentAmount: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '不能为空', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    // 路径上的路由id值
    routeId() {
      return this.$route.query.id
    }
  },
  created() {
    if (this.routeId) {
      this.getEditMonthCardInfo()
    }
  },
  methods: {
    // 根据routeid获取月卡信息的方法
    async getEditMonthCardInfo() {
      const res = await getEditMonthCardInfoApi(this.routeId)
      if (res.code === 10000) {
        // 回显数据
        const { carBrand, carNumber, cardEndDate, cardStartDate, paymentAmount, paymentMethod, personName, phoneNumber, rechargeId, carInfoId } = res.data
        this.carInfoFormData = {
          personName,
          phoneNumber,
          carNumber,
          carBrand
        }
        this.recentCardPriceInfo = {
          timePickerData: [cardStartDate, cardEndDate],
          paymentAmount,
          paymentMethod
        }
        this.recentCardPriceInfoUpload.cardEndDate = cardEndDate
        this.recentCardPriceInfoUpload.cardStartDate = cardStartDate
        this.carInfoId = carInfoId
        this.rechargeId = rechargeId
      }
    },
    // 日期选择器改变时的事件
    timePickerChange() {
      if (this.recentCardPriceInfo.timePickerData) {
        // console.log(this.recentCardPriceInfo.timePickerData)
        this.recentCardPriceInfoUpload.cardStartDate = this.recentCardPriceInfo.timePickerData[0]
        this.recentCardPriceInfoUpload.cardEndDate = this.recentCardPriceInfo.timePickerData[1]
      }
    },
    // 点击确认，提交表单里的数据时
    uploadConfirm() {
      const cardInfoForm = this.$refs.carInfoForm
      const recentCardPriceInfoForm = this.$refs.recentCardPriceInfoForm

      cardInfoForm.validate((flag) => {
        if (!flag) return
        recentCardPriceInfoForm.validate(async(flag) => {
          if (!flag) return
          // 所有内容校验成功后，合并所有数据，调用接口上传数据
          this.recentCardPriceInfoUpload.paymentAmount = this.recentCardPriceInfo.paymentAmount
          this.recentCardPriceInfoUpload.paymentMethod = this.recentCardPriceInfo.paymentMethod
          const addMonthCardData = Object.assign({}, this.carInfoFormData, this.recentCardPriceInfoUpload)
          if (this.routeId) {
            addMonthCardData.carInfoId = this.carInfoId
            addMonthCardData.rechargeId = this.rechargeId
            console.log(addMonthCardData)
            await putEditMonthCardInfoApi(addMonthCardData)
            this.$message({ type: 'success', message: '编辑成功' })
          } else {
            await addMonthCardApi(addMonthCardData)
            this.$message({ type: 'success', message: '添加成功' })
          }

          this.$router.back()
        })
      })
    },
    // 点击重置按钮，重置所有表单
    resetForm() {
      const cardInfoForm = this.$refs.carInfoForm
      const recentCardPriceInfoForm = this.$refs.recentCardPriceInfoForm
      cardInfoForm.resetFields()
      recentCardPriceInfoForm.resetFields()
    },
    // 自定义校验规则：车牌
    validatorCarNumber(rule, value, callback) {
      const pattern = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/
      if (pattern.test(value)) {
        callback()
      } else {
        callback(new Error('车牌号码格式不正确'))
      }
    }
  }

}
</script>

<template>
  <div class="container">
    <div class="top">
      <div class="left">
        <i class="el-icon-back" @click="$router.back()" />
        <span class="back" @click="$router.back()">返回</span>
        <span class="divide">|</span>
        <div class="add">{{ routeId?'编辑月卡':'添加月卡' }}</div>
      </div>
      <div class="right">
        <div class="userInfo">
          管理员
        </div>
      </div>
    </div>
    <div class="center">
      <div class="inner-one">
        <div class="inner-header">
          <span class="text">车辆信息</span>
        </div>
        <div class="form-body">
          <div class="inner-form-in">
            <el-form ref="carInfoForm" class="inner-form" label-width="30%" :model="carInfoFormData" :rules="carInfoRule">
              <el-form-item prop="personName" label="车主姓名:" style="width: 50%;">
                <el-input v-model="carInfoFormData.personName" style="width: 95%;" class="input" size="medium" placeholder="请输入车主姓名" />
              </el-form-item>
              <el-form-item prop="phoneNumber" label="联系方式:" style="width: 50%;">
                <el-input v-model="carInfoFormData.phoneNumber" style="width: 95%;" class="input" size="medium" placeholder="请输入联系方式" />
              </el-form-item>
              <el-form-item prop="carNumber" label="车牌号码:" style="width: 50%;">
                <el-input v-model="carInfoFormData.carNumber" style="width: 95%;" class="input" size="medium" placeholder="请输入车牌号码" />
              </el-form-item>
              <el-form-item prop="carBrand" label="车辆品牌:" style="width: 50%;">
                <el-input v-model="carInfoFormData.carBrand" style="width: 95%;" class="input" size="medium" placeholder="请输入车辆品牌" />
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <div class="inner-one">
        <div class="inner-header">
          <span class="text">最近一次月卡缴费信息</span>
        </div>
        <div class="form-body">
          <div class="inner-form-in">
            <el-form ref="recentCardPriceInfoForm" class="inner-form" label-width="30%" :model="recentCardPriceInfo" :rules="recentPriceRule">
              <el-form-item label="有效日期:" style="width: 50%;" prop="timePickerData">
                <el-date-picker
                  v-model="recentCardPriceInfo.timePickerData"
                  type="daterange"
                  range-separator="→"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="width: 95%;"
                  size="medium"
                  value-format="yyyy-MM-dd"
                  @change="timePickerChange"
                />
              </el-form-item>
              <el-form-item prop="paymentAmount" label="支付金额:" style="width: 50%;">
                <el-input v-model="recentCardPriceInfo.paymentAmount" style="width: 95%;" class="input" size="medium" placeholder="请输入支付金额" />
              </el-form-item>
              <el-form-item prop="paymentMethod" label="支付方式:" style="width: 50%;">
                <el-select v-model="recentCardPriceInfo.paymentMethod" size="medium" style="width: 95%;">
                  <el-option v-for="item in PAY_METHOD" :key="item.value" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="center">
        <el-button size="small" @click="resetForm">重置</el-button>
        <el-button type="primary" size="small" @click="uploadConfirm">确定</el-button>

      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container {
  background-color: #f4f6f8;
  width:100%;
  height: 100%;
  font-size: 16px;
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 64px;
    background-color: #fff;
    .left {
      display: flex;
      align-items: center;
      margin-left: 20px;
      .el-icon-back {
        font-size: 19px;
        margin-right: 5px;
      }
      .back {
        cursor: pointer;
      }
      .divide {
        margin: auto 10px;
      }
    }
    .right {
      margin-right: 2%;
      .text {
        font-size: 16px
      }
    }
  }
  .center{
      .inner-one {
        width: 85%;
        margin: 24px auto;
        background-color: #fff;
        height: 215px;
        .inner-header{
          // background-color: palevioletred;
          width: 100%;
          height: 58px;
          border-bottom: 1px solid #f4f6f8;
          .text {
            line-height: 58px;
            margin-left: 20px;
          }
        }
        .form-body{
          display: flex;
          padding: 24px;
          height: 157px;
          justify-content: center;
          .inner-form-in {
            // background-color: yellow;
            width:100%;
            .inner-form {
              display: flex;
              flex-wrap: wrap;
              ::v-deep .el-form-item__label {
                font-weight: 400;
                color: #000000d9;
              }
            }
          }

        }
      }

  }
  .bottom {
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    bottom: 0;
    height: 80px;
    width: 100%;
    .center {
      transform: translateY(10%);
      height: 50%;
      width: 10%;
    }
  }
}
</style>

